package com.fanshuai.algorithms.datastructure.linear.linkedlist;

/**
 * 单链表翻转
 */
public class ReverseList {

    public static List buildList(int[] arr) {
        List list = new List();
        for (int v : arr) {
            list.addLast(v);
        }

        return list;
    }

    public static List reverseList(List list) {
        if (null == list || null == list.head) {
            return new List();
        }

        List list1 = new List();
        List.Node node = list.head;

        while (node != null) {
            list1.addFirst(node.value);
            node = node.next;
        }

        return list1;
    }

    public static String outputList(List list) {
        if (null == list || null == list.head) {
            return "NULL";
        }

        StringBuilder b = new StringBuilder();
        List.Node node = list.head;
        while (node != null) {
            b.append(node.value).append("->");
            node = node.next;
        }
        b.append("NULL");

        return b.toString();
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 5, 7, 9, 4};
        List list = buildList(a);
        List list1 = reverseList(list);

        System.out.println(outputList(list));
        System.out.println(outputList(list1));
    }
}
